package com.example.store.service.impl;

import com.example.store.entity.Cart;
import com.example.store.entity.Product;
import com.example.store.mapper.ProductMapper;
import com.example.store.service.ICartService;
import com.example.store.mapper.CartMapper;
import com.example.store.service.ex.AccessDeniedException;
import com.example.store.service.ex.CartNotFoundException;
import com.example.store.service.ex.InsertException;
import com.example.store.service.ex.UpdataException;
import com.example.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    //购物车的业务依赖于购物车以及商品的持久层
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    public Void addToCart(Integer uid , Integer pid , Integer amount , String username){
        Cart res = cartMapper.findByUidAndPid(uid, pid);
        Date date = new Date();
        if(res==null){
            Cart cart = new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice()); //可能需要核对一下商品数据是否存在
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedTime(date);
            cart.setModifiedUser(username);
            Integer rows = cartMapper.insertCart(cart);
            if(rows!=1){
                throw new InsertException("添加购物车商品数据异常");
            }
        }else {
            amount+=res.getNum()==null ? 0 : res.getNum();
            Integer rows = cartMapper.updateCart(res.getCid(), amount, username, date);
            if(rows!=1){
                throw new UpdataException("更新购物车数据异常");
            }
        }
        return null;
    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartMapper.findByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart carts = cartMapper.findByCid(cid);
        if(carts==null){
            throw new CartNotFoundException("购物车数据不存在异常");
        }
        if(uid!=carts.getUid()){
            throw new AccessDeniedException("非法访问数据");
        }
        Integer num = carts.getNum()+1;
        Integer rows = cartMapper.updateCart(cid, num, username, new Date());
        if(rows!=1){
            throw new UpdataException("更新数据异常");
        }
        return num;
    }

    @Override
    public List<CartVO> getVOByCids(Integer uid, Integer[] cids) {
        List<CartVO> list = cartMapper.findVOByCid(cids);
        /**
         for (CartVO cart : list) {
         if (!cart.getUid().equals(uid)) {
         list.remove(cart);
         }
         }
         */
        //对集合的元素进行增加或移除时，使用集合的迭代器而不能使用使用 foreach。
        Iterator<CartVO> it = list.iterator();
        while (it.hasNext()) {
            CartVO cart = it.next();
            if (!cart.getUid().equals(uid)) {
                it.remove();
            }
        }
        return list;
    }
}
